package net.toocruel.iqismart.entity.core;

import net.toocruel.iqismart.entity.base.BaseEntity;
import net.toocruel.iqismart.entity.constants.SourceType;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.entity.validate.group.ControllerGroup;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/8 上午11:35
 */
@Entity
public class Comment extends BaseEntity {
    private static final long serialVersionUID = -3162065959655735248L;
    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,optional = false)
    @JoinColumn(name = "userId")
    private User user;

    private String content;

    @ManyToOne(cascade = CascadeType.MERGE,fetch = FetchType.LAZY)
    @JoinColumn(name = "questionId")
    private Question question;
    @Transient
    private Long questionId;

    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinColumn(name = "answerId")
    private Answer answer;
    @Transient
    private Long answerId;

    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinColumn(name = "articleId")
    private Article article;
    @Transient
    private Long articleId;

    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinColumn(name = "toCommentId")
    private Comment toComment;
    @Transient
    private Long toCommentId;

    @Enumerated(EnumType.ORDINAL)
    private SourceType sourceType;

    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @NotNull(groups = ControllerGroup.class)
    @JoinColumn(name = "toUserId")
    private User toUser;
    @Transient
    private Long toUserId;
    private int status;
    @OneToMany(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,mappedBy = "comment")
    private Set<Support> supports = new HashSet<>();
    @Transient
    private Boolean support;
    private byte device;

    private int supportNum;

    @Transient
    private Long sourceId;

    public int getSupportNum() {
        return supportNum;
    }

    public void setSupportNum(int supportNum) {
        this.supportNum = supportNum;
    }

    public Comment getToComment() {
        return toComment;
    }

    public void setToComment(Comment toComment) {
        this.toComment = toComment;
    }

    public Long getToCommentId() {
        return toCommentId;
    }

    public void setToCommentId(Long toCommentId) {
        this.toCommentId = toCommentId;
    }

    public Long getSourceId() {
        if(sourceType == SourceType.question && question!=null){
            return question.getId();
        }else if(sourceType == SourceType.answer && answer!=null){
            return answer.getId();
        }else if(sourceType == SourceType.article && article!=null){
            return article.getId();
        }
        return sourceId;
    }

    public void setSourceId(Long sourceId) {
        this.sourceId = sourceId;
    }

    public Long getToUserId() {
        return toUserId;
    }

    public void setToUserId(Long toUserId) {
        this.toUserId = toUserId;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Long getQuestionId() {
        return questionId;
    }

    public void setQuestionId(Long questionId) {
        this.questionId = questionId;
    }

    public Long getAnswerId() {
        return answerId;
    }

    public void setAnswerId(Long answerId) {
        this.answerId = answerId;
    }

    public Long getArticleId() {
        return articleId;
    }

    public void setArticleId(Long articleId) {
        this.articleId = articleId;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }

    public SourceType getSourceType() {
        return sourceType;
    }

    public void setSourceType(SourceType sourceType) {
        this.sourceType = sourceType;
    }

    public User getToUser() {
        return toUser;
    }

    public void setToUser(User toUser) {
        this.toUser = toUser;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }



    public byte getDevice() {
        return device;
    }

    public void setDevice(byte device) {
        this.device = device;
    }

    public Set<Support> getSupports() {
        return supports;
    }

    public void setSupports(Set<Support> supports) {
        this.supports = supports;
    }

    public Boolean getSupport() {
        User currentUser = null;
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if(principal instanceof User){
                currentUser = (User) principal;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(CollectionUtils.isNotEmpty(supports)){
            User finalCurrentUser = currentUser;
            supports.forEach(sp -> {
                if(sp.getUser() == finalCurrentUser){
                    support = true;
                }
            });
        }
        return support;
    }

    public void setSupport(Boolean support) {
        this.support = support;
    }
}
